package com.schema2uml;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class StringHelper {
	
	public static String typeName(final String name, final String length, final String scale) {
		final StringBuffer buffer = new StringBuffer();
		if (name != null) {
			buffer.append(name);
		}
		if (name != null && length != null && !name.matches(".+\\(.+\\)")) {
			buffer.append("(" + length);
			if (scale != null && Integer.getInteger(scale) != null ) {
				buffer.append("," + scale);
			}
			buffer.append(")");
		}
		return buffer.toString();
	}
	
	public static String className(String name, Map<String, String> replacementRules) {
		for (Map.Entry<String, String> me : replacementRules.entrySet()) {
			name = name.replaceAll(me.getKey(), me.getValue());
		}
		return camelCase(name);
	}

	public static String attributeName(String name) {
		return StringUtils.uncapitalize(camelCase(name));
	}
	
	public static String camelCase(String name) {
		StringBuffer buffer = new StringBuffer();
		String[] tokens = name.split("_|\\s+");
		if (tokens != null && tokens.length > 0) {
			for (int i = 0; i < tokens.length; i++) {
				buffer.append(StringUtils.capitalize(tokens[i].toLowerCase()));
			}
		} else {
			buffer.append(StringUtils.capitalize(name.toLowerCase()));
		}
		return buffer.toString();
	}

	public static String foreignKeyAttributeName(String fkColumnName) {
		return attributeName(fkColumnName.replaceAll("(?!\\.+)_ID$", ""));
	}

}
